package com.fang.controller;

import com.fang.dto.TestDTO;
import com.fang.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

/**
 * @Description
 * @Author Mr.Wang
 * @Date 2024-12-23 17:21
 **/
// @Validated
@RestController
public class TestController {
    private TestService testService;

    @Autowired
    public void setTestService(TestService testService) {
        this.testService = testService;
    }
    
    @PostMapping("/test-validation")
    public String testValidation(@RequestBody @Validated TestDTO testDTO) {
        return "success";
    }

    @GetMapping("/{num}")
    public Integer detail(@PathVariable("num")  @Min(2) @Max(3) Integer num) {
        return num * num;
    }

    /**
     * @PathVariable 使用了@Min
     * @RequestParam 使用了@Email
     * 在controller的方法的参数上，使用@Min注解的时候，@Validated注解需要放在controller上，才会起效果
     */
    @GetMapping("/getByEmail")
    public TestDTO getByAccount(@RequestParam  @NotBlank @Email String email) {
        TestDTO testDTO = new TestDTO();
        testDTO.setEmail(email);
        return testDTO;
    }

    @GetMapping("/returnString")
    public String returnString() {
        return "success";
    }
    @GetMapping("/returnNull")
    public String returnNull() {
        return null;
    }
}

